#include<iostream>
#include"epoller.hpp"
#include"listener.hpp"
#include"reactor.hpp"

int main(int argc,char* argv[]){
    if(argc!=2){
        std::cout<<"Usage: "<<argv[0]<<" port"<<std::endl;
        return 1;
    }
    int port=atoi(argv[1]);
    Reactor reactor;
    Listener* listener=new Listener(port);listener->Listen();
    listener->SetReactor(&reactor);
    Channel* channel=new Channel(listener->GetFd());
    // std::cout<<listener->GetFd()<<std::endl;
    channel->SetReactor(&reactor);
    channel->SetConnection((Connection*)listener);
    epoll_event event;
    event.events=EPOLLIN|EPOLLET;
    event.data.fd= listener->GetFd();
    //event.data.ptr=listener;
    if(reactor.AddChannel(listener->GetFd(),channel,&event,new InetAddr)){
        std::cout<<"监听端口成功"<<std::endl;
    }
    else{
        std::cout<<"监听端口失败"<<std::endl;
        return 1;
    }
    reactor.Run();
}